package com.lz.seata.service.impl;

import com.lz.seata.dao.IProductDao;
import com.lz.seata.domain.Product;
import com.lz.seata.service.IProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Description: ProductServiceImpl
 * @Date: 2020/5/4 11:37
 * @Author: LiZhen
 * @Version: 1.0
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Resource
    private IProductDao productDao;

    @Override
    public Product findById(Integer pid) {
        Optional<Product> byId = productDao.findById(pid);
        return byId.get();
    }

    @Override
    public void deductStock(Integer pid, Integer number) {
        Product product  = productDao.findById(pid).get();
        product.setStock(product.getStock() - number);
        productDao.save(product);
    }
}
